#! /bin/sh
#
# $Id: calpont-build 421 2007-14-15 15:46:55Z dhill $
#
# build calpont rpm and install on call6500
# $1 - release number or 'Latest'
# $2 - svn branch
#
if [ "$2" = "" ] ; then
	echo "enter release and svn-branch arguments"
	exit 0
fi
# !!! set for OS type 32 or 64 bit !!!

OS=x86_64

#
REL=$1
DIR=$2

#remove mysqld before new one is built
#prevents old version from being packaged in new build
rm -f /usr/local/Calpont/mysql/libexec/*

if [ $DIR = "genii" ] ; then
	./mysql-build-genii > mysql-build-genii.log &
else
	./mysql-build-branch $DIR > mysql-build-$DIR.log &
fi
#
HOME=/home/nightly/$DIR
rm -rf $HOME
rm -fr /home/nightly/rpm/
#

if [ $DIR = "genii" ] ; then
	su - nightly -c "export CXXFLAGS='-O3  -fno-strict-aliasing ';mkdir $DIR;updateGenii.pl > ~/$DIR/updateGenii.log 2>&1;cd ~/$DIR;make clean;make > ~/$DIR/make.log 2>&1;make > ~/$DIR/make1.log 2>&1;./build/build_rpms > ~/$DIR/build_rpms.log 2>&1"
else
 	su - nightly -c "export CXXFLAGS='-O3  -fno-strict-aliasing ';mkdir $DIR;updateGenii.pl -b $DIR > ~/$DIR/updateGenii.log 2>&1;cd ~/$DIR;make clean;make > ~/$DIR/make.log 2>&1;make > ~/$DIR/make1.log 2>&1;./build/build_rpms > ~/$DIR/build_rpms.log 2>&1"
fi

#
# generate rpm info text files

if [ ! -f $HOME/build/releasenum ]; then
	#default
	version=3.0
else
	. $HOME/build/releasenum
fi

cd /home/nightly/rpm/ > /dev/null 2>&1
cd RPMS/$OS/ > /dev/null 2>&1
test -f infinidb-platform-$version*.rpm || echo "no rpm"
test -f infinidb-platform-$version*.rpm || exit -1
rpm -qi -p infinidb-platform-$version*.rpm >  infinidb-platform-$OS-rpm-info.txt
echo " " >> infinidb-platform-$OS-rpm-info.txt
echo "MD5SUM" >>  infinidb-platform-$OS-rpm-info.txt
md5sum  infinidb-platform-$version*.rpm >>  infinidb-platform-$OS-rpm-info.txt
echo " " >>  infinidb-platform-$OS-rpm-info.txt
#
test -f infinidb-storage-engine-*.rpm || echo "no rpm"
test -f infinidb-storage-engine-*.rpm || exit -1
rpm -qi -p infinidb-storage-engine-*.rpm >  infinidb-storage-engine-$OS-rpm-info.txt
echo " " >>  infinidb-storage-engine-$OS-rpm-info.txt
echo "MD5SUM" >>  infinidb-storage-engine-$OS-rpm-info.txt
md5sum  infinidb-storage-engine-*.rpm >>  infinidb-storage-engine-$OS-rpm-info.txt
echo " " >>  infinidb-storage-engine-$OS-rpm-info.txt
#
test -f infinidb-mysql-*.rpm || echo "no rpm"
test -f infinidb-mysql-*.rpm || exit -1
rpm -qi -p infinidb-mysql-*.rpm >  infinidb-mysql-$OS-rpm-info.txt
echo " " >>  infinidb-mysql-$OS-rpm-info.txt
echo "MD5SUM" >>  infinidb-mysql-$OS-rpm-info.txt
md5sum  infinidb-mysql-*.rpm >>  infinidb-mysql-$OS-rpm-info.txt
echo " " >>  infinidb-mysql-$OS-rpm-info.txt
#
{
if [ $REL != "Latest" ] ; then
	# copy rpms to calweb
        DATE=`date +%F`
	cd /home/nightly/rpm/RPMS/$OS/
        smbclient //calweb/shared -Wcalpont -Uoamuser%Calpont1 -c "cd Iterations;mkdir $REL;cd $REL;rm *.$OS.rpm;prompt OFF;mput *"
        smbclient //calweb/shared -Wcalpont -Uoamuser%Calpont1 -c "cd Iterations/nightly;mkdir $REL-$DATE;cd $REL-$DATE;rm *.$OS.rpm;prompt OFF;mput *"
        cd /home/nightly/$DIR/export/etc/
        smbclient //calweb/shared -Wcalpont -Uoamuser%Calpont1 -c "cd Iterations/$REL;rm Calpont.xml;prompt OFF;mput Calpont.xml"
        smbclient //calweb/shared -Wcalpont -Uoamuser%Calpont1 -c "cd Iterations/nightly/$REL-$DATE;rm Calpont.xml;prompt OFF;mput Calpont.xml"

else
	# copy nightly on calweb
	DATE=`date +%F`
	cd /home/nightly/rpm/RPMS/$OS/
	smbclient //calweb/shared -Wcalpont -Uoamuser%Calpont1 -c "cd Iterations/Latest;rm *.$OS.rpm;prompt OFF;mput *"
	smbclient //calweb/shared -Wcalpont -Uoamuser%Calpont1 -c "cd Iterations/nightly;mkdir genii-$DATE;cd genii-$DATE;prompt OFF;mput *"
	cd /home/nightly/$DIR/export/etc/
	smbclient //calweb/shared -Wcalpont -Uoamuser%Calpont1 -c "cd Iterations/Latest;rm Calpont.xml;prompt OFF;mput Calpont.xml"
	smbclient //calweb/shared -Wcalpont -Uoamuser%Calpont1 -c "cd Iterations/nightly/genii-$DATE;prompt OFF;mput Calpont.xml"
fi
} 2> /root/infinidb-build-$REL-$DIR.log
#
echo "Build Successfully Completed"
exit 0
